<template>
  <div class="l-tabel">
    <el-table
      :data="data"
      tooltip-effect="dark"
      :header-cell-style="headerCellStyle"
      style="width: 100%"
      :cell-style="cellStyle"
      :row-style="rowStyle"
      :height="height"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="60" fixed v-if="selectFlag" />
      <el-table-column label="序号" width="60" v-if="paginationFlag">
        <template #default="scope">
          {{ scope.$index + 1 + (pagination.page - 1) * pagination.pageSize }}
        </template>
      </el-table-column>
      <el-table-column label="序号" width="60" v-else>
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <template v-for="(item, index) in columns" :key="index">
        <el-table-column v-if="!item.slot" v-bind="item.attar"> </el-table-column>
        <el-table-column v-else v-bind="item.attar">
          <template #default="scope">
            <slot :name="item.slot" :scope="scope"></slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="pagination" v-if="paginationFlag">
      <el-pagination
        v-model:currentPage="pagination.page"
        v-model:page-size="pagination.pageSize"
        :page-sizes="pagination.pageSizes"
        background
        layout="sizes,prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
        <span style="margin-right: 5px">当前共{{ total }}条</span>
      </el-pagination>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
const props = defineProps({
  data: {
    type: Array,
    default: []
  },
  columns: {
    type: Array,
    default: []
  },
  rowStyle: {
    type: Object,
    default: { height: '50px' }
  },
  cellStyle: {
    type: Object,
    default: { textAlign: 'center' }
  },
  headerCellStyle: {
    type: Object,
    default: {
      textAlign: 'center',
      backgroundColor: '#f5f7fa'
    }
  },
  height: {
    type: Number,
    default: 500
  },
  selectFlag: {
    type: Boolean,
    default: false
  },
  paginationFlag: {
    type: Boolean,
    default: false
  },
  pagination: {
    type: Object,
    default: {
      page: 1,
      pageSize: 30,
      pageSizes: [30, 50, 100, 300]
    }
  },
  total: {
    type: Number,
    default: 0
  }
})
const emit = defineEmits(['selectionChange', 'sizeChange', 'currentChange'])

const handleSelectionChange = (val: any[]) => {
  emit('selectionChange', val)
}
const handleSizeChange = (val: number) => {
  // console.log(`${val} items per page`)
  emit('sizeChange', val)
}
const handleCurrentChange = (val: number) => {
  emit('currentChange', val)
}
</script>

<style scoped lang="less">
.pagination {
  margin: 15px;
  display: flex;
  justify-content: right;
}
</style>
